//package com.thd.springboot.framework.web.controller;
//
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.thd.springboot.framework.model.Message;
//import com.thd.springboot.framework.utils.JacksonUtil;
//import org.springframework.core.MethodParameter;
//import org.springframework.http.MediaType;
//import org.springframework.http.converter.HttpMessageConverter;
//import org.springframework.http.server.ServerHttpRequest;
//import org.springframework.http.server.ServerHttpResponse;
//import org.springframework.http.server.ServletServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.web.bind.annotation.ControllerAdvice;
//import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
//
//import javax.servlet.http.HttpServletResponse;
//
///**
// * 统一返回对象
// *
// * @author: wanglei62
// * @DATE: 2022/10/12 16:11
// **/
//@ControllerAdvice
//public class ResponseBodyHandlerController implements ResponseBodyAdvice<Object> {
//    @Override
//    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
//        return true;
//    }
//
//    @Override
//    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
//        // 因为String返回类型的处理器StringHttpMessageConverter只能接收String类型，为了保证返回封装的一致性，这里需要手动转换，并设置返回报文格式为json
//        if (o instanceof String) {
//            try {
//                HttpServletResponse servletResponse = ((ServletServerHttpResponse) serverHttpResponse).getServletResponse();
//                servletResponse.setCharacterEncoding("UTF-8");
//                servletResponse.setContentType("application/json; charset=utf-8");
//                return JacksonUtil.objToJson(Message.success(o));
//            } catch (Exception e) {
//                throw e;
//            }
//        }
//
//        if (o instanceof Message) {
//            return o;
//        }
//
//
//        return Message.success(o);
//    }
//}
